import { BaseApiService } from '../base/api.service';
import type {
  CommitDistribution,
  DailyDistribution,
  WeeklyStats,
  HourlyStats,
  MonthlyStats,
} from '@/types/analysis/statistics';
import type { BaseInfo } from '@/types/business/commit';
import type { RepoAuthorStats } from '@/types/analysis/statistics';

/**
 * 统计服务类
 */
export class StatisticsService extends BaseApiService {
  constructor() {
    super('/api');
  }

  /**
   * 获取基础信息
   */
  async getBaseInfo(): Promise<BaseInfo> {
    return this.get<BaseInfo>('baseinfo/get-base-info');
  }

  /**
   * 获取提交分布统计
   */
  async getCommitDistribution(): Promise<CommitDistribution[]> {
    return this.get<CommitDistribution[]>('gitlog/stats/commit-distribution');
  }

  /**
   * 获取每日工程师分布
   */
  async getDailyEngineerDistribution(): Promise<DailyDistribution[]> {
    return this.get<DailyDistribution[]>('gitlog/stats/daily-engineer-distribution');
  }

  /**
   * 获取周统计
   */
  async getWeeklyStats(engineerId = null): Promise<WeeklyStats[]> {
    if (engineerId) {
      return this.query<WeeklyStats[]>('gitlog/stats/weekly', { engineerId });
    }
    return this.get<WeeklyStats[]>('gitlog/stats/weekly');
  }

  /**
   * 获取小时统计
   */
  async getHourlyStats(engineerId = null): Promise<HourlyStats[]> {
    if (engineerId) {
      return this.query<HourlyStats[]>('gitlog/stats/hourly', { engineerId });
    }
    return this.get<HourlyStats[]>('gitlog/stats/hourly');
  }

  /**
   * 获取工程师月度统计
   */
  async getEngineerMonthlyStats(engineerId: string): Promise<MonthlyStats[]> {
    return this.query<MonthlyStats[]>('gitlog/stats/engineer-monthly', { engineerId });
  }

  /**
   * 获取仓库月度统计
   */
  async getRepoMonthlyStats(gitInstance: string, repositoryId: number): Promise<MonthlyStats[]> {
    return this.query<MonthlyStats[]>('gitlog/stats/repo-monthly', { gitInstance, repositoryId });
  }

  /**
   * 获取仓库作者统计
   */
  async getRepoAuthorsStats(gitInstance: string, repoId: string): Promise<RepoAuthorStats[]> {
    return this.query<RepoAuthorStats[]>('gitlog/stats/repo-authors', { gitInstance, repoId });
  }
}

// 导出单例实例
export const statisticsService = new StatisticsService();
